package cn.com.godliu.rabbitmqprovider.controller;

import cn.com.godliu.rabbitmqprovider.common.DateUtil;
import cn.com.godliu.rabbitmqprovider.config.JRedisConfig;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Slf4j
@RestController
public class MQMessageController {

    @Autowired
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法

    private Map<String, Object> getRandomMsg(String message) {
        String messageId = String.valueOf(UUID.randomUUID());
        String createTime = DateUtil.getSystem();
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", message);
        map.put("createTime", createTime);

        return map;
    }

    @PostMapping(value = "/sendDirectMessage", produces = {"application/json;charset=UTF-8"})
    public String sendDirectMessage(String message) {
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        Map<String, Object> map = getRandomMsg(message);
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);


        return new Gson().toJson(map);
    }

    @PostMapping(value = "/sendDirectackMessage", produces = {"application/json;charset=UTF-8"})
    public String sendDirectackMessage(String message) {
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        Map<String, Object> map = getRandomMsg(message);
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);


        return new Gson().toJson(map);
    }

    @PostMapping(value = "/sendFanoutMessage", produces = {"application/json;charset=UTF-8"})
    public String sendFanoutMessage(String message) {
        Map<String, Object> map = getRandomMsg(message);
        rabbitTemplate.convertAndSend("fanoutExchange", null, map);
        return new Gson().toJson(map);
    }

    @PostMapping(value = "/sendTopicMessage1", produces = {"application/json;charset=UTF-8"})
    public String sendTopicMessage1(String msg) {
        Map<String, Object> map = getRandomMsg(msg);
        //rabbitTemplate.convertAndSend("topicExchange", "man", msg);
        // 订阅主题是topic.man才能收到

        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
        Message message = new Message(new Gson().toJson(map).getBytes(), messageProperties);
        rabbitTemplate.convertSendAndReceive("topicExchange", "man", message);

        return new Gson().toJson(map);
    }

    @PostMapping(value = "/sendTopicMessage2", produces = {"application/json;charset=UTF-8"})
    public String sendTopicMessage2(String message) {
        Map<String, Object> map = getRandomMsg(message);
        // 订阅主题是topic.man才能收到
        rabbitTemplate.convertAndSend("topicExchange", "woman", map);
        return new Gson().toJson(map);
    }

    @PostMapping(value = "/sendTopicMessage3", produces = {"application/json;charset=UTF-8"})
    public String sendTopicMessage3(String message) {
        Map<String, Object> map = getRandomMsg(message);
        rabbitTemplate.convertAndSend("topicExchange", "both", map);
        return new Gson().toJson(map);
    }

    @PostMapping(value = "/sendTopicMessage4", produces = {"application/json;charset=UTF-8"})
    public String sendTopicMessage4(String message) {
        Map<String, Object> map = getRandomMsg(message);
        rabbitTemplate.convertAndSend("topicExchange", "wobig", map);
        return new Gson().toJson(map);
    }

    /**
     * 测试不存在的exchange，RabbitConfig里的ConfirmCallback会触发不
     *
     * @param message
     * @return
     */
    @PostMapping(value = "/noexchange", produces = {"application/json;charset=UTF-8"})
    public String noexchange(String message) {
        Map<String, Object> map = getRandomMsg(message);
        rabbitTemplate.convertAndSend("noexchange", "wobig", map);
        return new Gson().toJson(map);
    }

    /**
     * 测试不存在的routingkey，RabbitConfig里的ConfirmCallback会触发不
     *
     * @param message
     * @return
     */
    @PostMapping(value = "/noroutingkey", produces = {"application/json;charset=UTF-8"})
    public String noroutingkey(String message) {
        Map<String, Object> map = getRandomMsg(message);
        rabbitTemplate.convertAndSend("TestDirectExchange", "noroutingkey", map);
        return new Gson().toJson(map);
    }

    /**
     * 测试不存在的routingkey，RabbitConfig里的ConfirmCallback会触发不
     *
     * @param message
     * @return
     */
    @PostMapping(value = "/noexchangeandroutingkey", produces = {"application/json;charset=UTF-8"})
    public String noexchangeandroutingkey(String message) {
        Map<String, Object> map = getRandomMsg(message);
        rabbitTemplate.convertAndSend("noexchangeandroutingkey", "noroutingkey", map);
        return new Gson().toJson(map);
    }
}
